package com.uenpay.im.ui.old.activity.company;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-2-22 15:47:38
 * @copyRight http://www.qiyunxin.com
 */
public class DepartmentsListActivity extends Activity {

	private View loading;
	private int entid;
	private CompanyHandle mCompanyHandle = new CompanyHandle();
	private DepartmentsAdapter mDepartmentsAdapter = null;
	private ListView listview;
	private String superior_department;
	private boolean is_manager = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_layout);
		IMApplication.getInstance().addActivity(this);
		entid = getIntent().getIntExtra("entid", 0);
		superior_department = getIntent().getStringExtra("superior_department");
		is_manager = getIntent().getBooleanExtra("is_manager", false);
		initView();
		initListener();
		getData();
	}

	private void initView() {
		((TextView) findViewById(R.id.title_textview))
				.setText(R.string.department_manager);
		loading = findViewById(R.id.loading);
		listview = (ListView) findViewById(R.id.listview);
		if (is_manager) {
			((TextView) findViewById(R.id.title_right_tv))
					.setText(R.string.add);
			findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
		}
	}

	private void initListener() {
		findViewById(R.id.back_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						finish();
					}
				});
		findViewById(R.id.title_right_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						DepartmentEntity mDepartmentEntity = new DepartmentEntity();
						mDepartmentEntity.entid = entid;
						mDepartmentEntity.pid = 0;
						mDepartmentEntity.pdepartname = superior_department;
						Bundle bundle = new Bundle();
						bundle.putSerializable("departmentEntity",
								mDepartmentEntity);
						ActivityHandle.startActivity(
								DepartmentsListActivity.this,
								CreateDepartmentsActivity.class, bundle, 100);

					}
				});
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				DepartmentEntity departmentEntity = (DepartmentEntity) arg0
						.getAdapter().getItem(arg2);
				if (departmentEntity != null) {
					Bundle bundle = new Bundle();
					bundle.putSerializable("departmentEntity", departmentEntity);
					bundle.putBoolean("is_manager", is_manager);
					ActivityHandle.startActivity(DepartmentsListActivity.this,
							SubsectorsActivity.class, bundle, 100);
				}
			}
		});
	}

	private void getData() {
		loading.setVisibility(View.VISIBLE);
		mCompanyHandle.getDepartByPidList(0, entid, new CompanyHandle.IGetDepartListResult() {

			@Override
			public void onGetDepartResult(int status, String msg,
					ArrayList<DepartmentEntity> arrayList) {
				loading.setVisibility(View.GONE);
				mDepartmentsAdapter = new DepartmentsAdapter(
						DepartmentsListActivity.this, arrayList, false);
				listview.setAdapter(mDepartmentsAdapter);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 100 && resultCode == RESULT_OK) {
			getData();
		} else if (requestCode == 100 && resultCode == 2) {
			finish();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onBackPressed() {
		finish();
		super.onBackPressed();
	}

	@Override
	protected void onDestroy() {
		IMApplication.getInstance().removeActivity(this);
		super.onDestroy();
	}
}
